﻿#!/bin/sh
#author          :Ricardo de Souza Maia
#license         :GPLv3
#version         :$Id  
#encoding        :UTF-8
#Based on GNU/Linux Debian script.

function add_apache_instance()
{
	set -eu

	if [ $# != 1 ] ; then
			echo usage: $0 '<suffix>'
			exit 1
	fi

	# the SUFFIX must not contain spaces or shell meta characters
	SUFFIX=$1

	if [ -e $APACHE_ROOT-$SUFFIX ] ; then
			echo ERROR: $APACHE_ROOT-$SUFFIX already exists
			exit 2
	fi

	echo Setting up $APACHE_ROOT-$SUFFIX ...
	cp -a $APACHE_ROOT $APACHE_ROOT-$SUFFIX

	echo Setting up /etc/init.d/httpd-$SUFFIX ...
	cp $CURRENT_DIR/secondary_script.sh /etc/init.d/httpd-$SUFFIX
	# adjust service name (this prevents us from using a simple symlink)
	perl -p -i -e s,XXX,$SUFFIX, /etc/init.d/httpd-$SUFFIX
	chmod 755 /etc/init.d/httpd-$SUFFIX

	echo -n Setting up symlinks:
	echo -n "apachectl-$SUFFIX"
	ln -s /usr/sbin/apachectl /usr/local/sbin/apachectl-$SUFFIX
	echo

	echo Setting up /etc/logrotate.d/httpd-$SUFFIX and /var/log/httpd-$SUFFIX ...
	cp -a /etc/logrotate.d/httpd /etc/logrotate.d/httpd-$SUFFIX
	perl -p -i -e s,httpd,httpd-$SUFFIX,g /etc/logrotate.d/httpd-$SUFFIX
	mkdir /var/log/httpd-$SUFFIX
	chmod 750 /var/log/httpd-$SUFFIX
	chown root:adm /var/log/httpd-$SUFFIX
	
	#Remove os websites copiados da instancia padrão.
	rm $APACHE_ROOT-$SUFFIX/virtual_hosts/*
}
